package com.qymerge.manage.data.dao.impl;

import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.constant.ParamKey;
import com.qymerge.manage.core.dao.BaseDao;
import com.qymerge.manage.data.dao.IProductPrizeDao;
import com.qymerge.manage.data.dto.ProductPrizeDto;
import com.qymerge.manage.data.ro.ProductPrizeRO;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.util.*;

/**
 *<p>Title: ProductDaoImpl</p>
 *<p>Description: 限定礼遇</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/12/19
 *@Time 17:39
 */
@Repository
public class ProductPrizeDaoImpl extends BaseDao implements IProductPrizeDao {

    /**
     * 查询所有分页
     *
     * @param ro
     * @param pageRequest
     * @return
     */
    @Override
    public List list(ProductPrizeDto ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = ParamKey.NameSpace.TableSelect.productprize;
        NamedParameterJdbcTemplate jdbc = new NamedParameterJdbcTemplate(this.getJdbcTemplate());
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getProductid() > 0) {
            sql += " and productid=:productid";
            paramMap.put("productid", ro.getProductid());
        }

        if (StringUtils.isNotBlank(ro.getProductname())) {
            sql += " and productname like :productname";
            paramMap.put("productname", "%" + ro.getProductname() + "%");
        }

        if (StringUtils.isNotBlank(ro.getProductenname())) {
            sql += " and productname like :productname";
            paramMap.put("productname", "%" + ro.getProductenname() + "%");
        }

        if (StringUtils.isNotBlank(ro.getCreate_time_begin()) && StringUtils.isNotBlank(ro.getCreate_time_end())) {
            sql += " and adddate BETWEEN :create_time_begin AND :create_time_end";
            paramMap.put("create_time_begin", ro.getCreate_time_begin());
            paramMap.put("create_time_end", ro.getCreate_time_end());
        }

        // 分页数据获取
        sql += " order by productid,sortnum desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return jdbc.query(sql, paramMap, new BeanPropertyRowMapper<ProductPrizeRO>(ProductPrizeRO.class));
    }

    /**
     * 所有数量
     *
     * @param
     * @return
     */
    @Override
    public int total(ProductPrizeDto ro) {
        String sql = "select count(1) from productprize where 1=1";
        NamedParameterJdbcTemplate jdbc = new NamedParameterJdbcTemplate(this.getJdbcTemplate());
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getProductid() > 0) {
            sql += " and productid=:productid";
            paramMap.put("productid", ro.getProductid());
        }

        if (StringUtils.isNotBlank(ro.getProductname())) {
            sql += " and productname like :productname";
            paramMap.put("productname", "%" + ro.getProductname() + "%");
        }

        if (StringUtils.isNotBlank(ro.getProductenname())) {
            sql += " and productname like :productname";
            paramMap.put("productname", "%" + ro.getProductenname() + "%");
        }

        if (StringUtils.isNotBlank(ro.getCreate_time_begin()) && StringUtils.isNotBlank(ro.getCreate_time_end())) {
            sql += " and adddate BETWEEN :create_time_begin AND :create_time_end";
            paramMap.put("create_time_begin", ro.getCreate_time_begin());
            paramMap.put("create_time_end", ro.getCreate_time_end());
        }


        return jdbc.queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public ProductPrizeRO findOne(Long id) {
        StringBuffer sql = new StringBuffer(ParamKey.NameSpace.TableSelect.productprize);
        sql.append("and productid=?");
        RowMapper<ProductPrizeRO> rowMapper = new BeanPropertyRowMapper<ProductPrizeRO>(ProductPrizeRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, id);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public int install(ProductPrizeRO ro) {
        StringBuilder sql = new StringBuilder("insert into productprize(productname,productenname,pic,havenum,sendnum,adddate,sortnum) values(?,?,?,?,?,?,?)");

        List params = new ArrayList();
        params.add(ro.getProductname());
        params.add(ro.getProductenname());
        params.add(ro.getPic());
        params.add(ro.getHavenum());
        params.add(ro.getSendnum());
        params.add(ro.getAdddate());
        params.add(ro.getSortnum());

        return getJdbcTemplate().update(sql.toString(), params.toArray());
    }

    @Override
    public int update(ProductPrizeRO ro) {
        StringBuilder sql = new StringBuilder("update productprize set productname=?, productenname=?, pic=?, havenum=?, sendnum=?, adddate=?, sortnum=? ");

        List params = new LinkedList();
        params.add(ro.getProductname());
        params.add(ro.getProductenname());
        params.add(ro.getPic());
        params.add(ro.getHavenum());
        params.add(ro.getSendnum());
        params.add(ro.getAdddate());
        params.add(ro.getSortnum());

        sql.append("where productid=?");
        params.add(ro.getProductid());

        return getJdbcTemplate().update(sql.toString(), params.toArray());
    }

    @Override
    public int remove(List<Long> ids) {
        // 删除
        String sql="delete from productprize where productid=?";
        final List<Long> idss = ids;
        BatchPreparedStatementSetter setterMid = new BatchPreparedStatementSetter(){

            @Override
            public int getBatchSize(){
                return idss.size();
            }

            @Override
            public void setValues(PreparedStatement ps, int i){
                Long id = idss.get(i);
                try {
                    ps.setLong(1, id);
                } catch(Exception e){
                    logger.error("batchInsertWaitAward Exception:", e);
                }
            }
        };
        return getJdbcTemplate().batchUpdate(sql, setterMid).length;
    }

}
